package com.yc.baseapi;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class HandleDate {
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
    private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
    
    public static Date convert1(String str) {
		Date res = null;
		try {
			Thread.sleep(100);
			res = sdf.parse(str);
		} catch (ParseException | InterruptedException e) {
			e.printStackTrace();
		}
		
		return res;
	}
	
	public static Date convert(String str) {
		Date res = null;
		try {
			Thread.sleep(100);
			res = df.get().parse(str);
		} catch (ParseException | InterruptedException e) {
			e.printStackTrace();
		}
		
		return res;
	}
	
	public void testConvert1() {
		new Thread(() -> {
			String dataStr = "2019-01-01";
			System.out.println(convert1(dataStr));
			
		}).start();
		new Thread(() -> {
			String dataStr = "2019-01-02";
			System.out.println(convert1(dataStr));
			
		}).start();
		new Thread(() -> {
			String dataStr = "2019-01-03";
			System.out.println(convert1(dataStr));
			
		}).start();
	}
	
	public void testConvert() {
		new Thread(() -> {
			String dataStr = "2019-01-01";
			System.out.println(convert(dataStr));
			
		}).start();
		new Thread(() -> {
			String dataStr = "2019-01-02";
			System.out.println(convert(dataStr));
			
		}).start();
		new Thread(() -> {
			String dataStr = "2019-01-03";
			System.out.println(convert(dataStr));
			
		}).start();
	}
	
	public void testCompare() {
		String str1 = "2019-08-09";
		String str2 = "2019-08-10";
		String str3 = "2019-08-10";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date1 = sdf.parse(str1);
			Date date2 = sdf.parse(str2);
			Date date3 = sdf.parse(str3);
			
			System.out.println(date1.compareTo(date1));//0
			System.out.println(date1.compareTo(date2));//-1
			System.out.println(date2.compareTo(date1));//1
			System.out.println(date2.compareTo(date3));//0
			
			System.out.println("正常:" + str1);
			//减一天
			Calendar cal1 = Calendar.getInstance();  
			cal1.setTime(date1);  
			cal1.add(Calendar.DAY_OF_MONTH, -1);  
		    Date dt1 = cal1.getTime();
		    System.out.println("减一天:" + sdf.format(dt1));
		    //加两天
		    cal1.add(Calendar.DAY_OF_MONTH, 2);  
		    Date dt2 = cal1.getTime();
		    System.out.println("加两天:" + sdf.format(dt2));
			
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}

	public void m2() {
		String str1 = "2019-07-31";
		try {
			Date date1 = sdf.parse(str1);
			DateTime date2 = DateUtil.offsetMonth(date1, -1);
			System.out.println(sdf.format(date2));

		} catch (ParseException e) {
			e.printStackTrace();
		}


	}

	
	private boolean verifyUseableDate(Date usableLowerTime, Date usableUpperTime) {
		//有效期的存储格式： yyyy-MM-dd 00:00:00  ~ yyyy-MM-dd 00:00:00,无法覆盖上线边界值，所以+1天
		Calendar calendarUpper = Calendar.getInstance();
		calendarUpper.setTime(usableLowerTime);
		calendarUpper.add(Calendar.DATE,1);
		usableUpperTime = calendarUpper.getTime();
		
		Date now = new Date();
		if (now.after(usableLowerTime) && now.before(usableUpperTime))
			return true;
		return false;
	}
	
	public void testVerifyUseableDate() {
		String str1 = "2020-04-16 00:00:00";
		String str2 = "2020-04-17 00:00:00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date1 = sdf.parse(str1);
			Date date2 = sdf.parse(str2);
//			System.out.println(verifyUseableDate(date1, date2));
			
//			System.out.println(date1.after(date2));
			System.out.println(date1.compareTo(date2) <=0);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<String> getDays(String startTime, String endTime) {

		// 返回的日期集合
		List<String> days = new ArrayList<String>();

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd");
		try {
			Date start = dateFormat.parse(startTime);
			Date end = dateFormat.parse(endTime);

			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(start);

			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(end);
			tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
			while (tempStart.before(tempEnd)) {
				days.add(dateFormat2.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}
			days.add(dateFormat2.format(tempStart.getTime()));

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return days;
	}

	public void testGetDays() {
		List<String> list = getDays("2021-07-25", "2021-08-25");
		for (String str : list) {
			System.out.println(str);
		}

	}

	public void m3() {
		int pageIndex = 3;
		int pageSize = 15;

		List<String> allDays = getDays("2021-07-25", "2021-08-25");
		int total = allDays.size()-1;

		int startIndex = (pageIndex - 1) * pageSize;
		if (startIndex >= total) {
			System.out.println("----1-----");
		}
		int endIndex = startIndex + pageSize;
		if (endIndex >= total) {
			endIndex = total;
		}

		String startDate = allDays.get(startIndex);
		String endDate = allDays.get(endIndex);

		System.out.println("startDate=" + startDate);
		System.out.println("endDate=" + endDate);
	}


	private String offsetDay(String startDate, int offset) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			DateTime date = DateUtil.offsetDay(sdf.parse(startDate), offset);
			return date.toDateStr();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void testOffsetDay() {
		System.out.println(offsetDay("2021-06-11", 1));
	}

	/**
	 * 获取的年份 和实际的年份 一致
	 * 获取的月份 比实际 小1
	 */
	private void m4() {
		int month = DateUtil.month(new Date());
		System.out.println("month=" + month);
		int year = DateUtil.year(new Date());
		System.out.println("year=" + year);
	}

	public void m5() {
		Date now = new Date();
		System.out.println(now.getTime());

	}

	/**
	 * 和当前时间的相差的分钟
	 * @param hour 整点
	 */
	private int offSetMinute(int hour) {
		return Integer.parseInt(DateUtil.format(new Date(), "HHmm")) - Integer.parseInt(numStr(hour) + "00");
	}

	/**
	 * 整点变两位字符串
	 * @param hour
	 * @return
	 */
	private String numStr(int hour) {
		return (hour < 10)?("0"+hour):(""+hour);
	}


	public static void main(String[] args) {
		HandleDate hd = new HandleDate();
//		hd.testConvert();
//		hd.testConvert1();
//		hd.testCompare();
//		hd.testVerifyUseableDate();
//		hd.m2();
//		hd.testGetDays();
//		hd.m3();
//		hd.testOffsetDay();
//		hd.m4();
//		hd.m5();
		System.out.println(hd.offSetMinute(10));
	}

}
